<?php
namespace Admin\Controller;
use Common\Util\Tree;
use Common\Builder\ListBuilder;
use Common\Builder\FormBuilder;

class CollegeController extends AdminController
{
    private $model = 'college_category';
    /**
     * 分类列表
     */
    public function index()
    {
        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%'.$keyword.'%');
        $map = array(
            'id|title' => array($condition, $condition, '_multi' => true),
            'status' => array('egt', 0)
        );

        $categoryM = D('Admin/Category');
        $data = $categoryM->field('id, pid, title, icon, create_time, status')->where($map)->order('id desc')->select();

        $tree = new Tree();
        $data = $tree->toFormatTree($data);
        foreach($data as &$d) {
            switch($d['pid']) {
                case '0':
                    $d['ptitle'] = '学前营';
                    break;
                case '1':
                    $d['ptitle'] = '成长营';
                    break;
                case '2':
                    $d['ptitle'] = '成功营';
                    break;

            }
        }

        $attr['title'] = '编辑';
        $attr['class'] = 'label label-primary';
        $attr['href']  = U('Admin/college/edit', array('id' => '__data_id__'));

        $builder = new ListBuilder();
        $builder->setMetaTitle('分类列表')
            ->addTopButton('addnew', array('href' => U('Admin/college/add')))
            ->addTopButton('resume', array('model' => $this->model))
            ->addTopButton('forbid', array('model' => $this->model))
            ->setSearch('请输入ID/分类名称', U('Admin/college/index'))
            ->addTableColumn('ptitle', '上级分类')
            ->addTableColumn('title', '分类')
            ->addTableColumn('icon', '图标', 'icon')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data)
            ->addRightButton('self', $attr)
            ->addRightButton('hide', array('model' => $this->model))
            ->addRightButton('forbid', array('model' => $this->model))
            ->addRightButton('delete', array('model' => $this->model))
            ->display();
    }

    /**
     * 新增分类
     */
    public function add() {
        if(IS_POST) {
            $categoryM = D('Admin/Category');
            $data = $categoryM->create();
            if ($data) {
                $id = $categoryM->add();
                if ($id) {
                    $this->success('新增成功', U('Admin/college/index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($categoryM->getError());
            }
        } else {
            $builder = new FormBuilder();
            $builder->setMetaTitle('新增分类')
                ->setPostUrl(U('Admin/college/add'))
                ->addFormItem('pid', 'select', '上级分类', '所属的上级分类', array(0=>'学前营', 1=>'成长营', 2=>'成功营'))
                ->addFormItem('title', 'text', '分类标题', '分类标题')
                ->addFormItem('description', 'kindeditor', '描述', '分类描述', null, 'hidden')
                ->addFormItem('icon', 'icon', '图标', '菜单图标')
                ->display();
        }
    }

    /**
     * 编辑分类
     */
    public function edit($id) {
        if(IS_POST) {
            $categoryM = D('Admin/Category');
            $data = $categoryM->create();
            if ($data) {
                if ($categoryM->save() !== false) {
                    $this->success('更新成功', U('Admin/college/index'));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($categoryM->getError());
            }
        } else {
            $categoryM = D('Category');
            $data = $categoryM->find($id);
            $builder = new FormBuilder();
            $builder->setMetaTitle('编辑分类')
                ->setPostUrl(U('Admin/college/edit', array('id' => $id)))
                ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('pid', 'select', '上级分类', '所属的上级分类', array(0=>'学前营', 1=>'成长营', 2=>'成功营'))
                ->addFormItem('title', 'text', '分类标题', '分类标题')
                ->addFormItem('description', 'kindeditor', '描述', '分类描述', null, 'hidden')
                ->addFormItem('icon', 'icon', '图标', '菜单图标')
                ->setFormData($data)
                ->display();
        }
    }

    /**
     * 数据状态
     */
    public function setStatus($model = CONTROLLER_NAME) {
        $ids = I('request.ids');
        $status = I('request.status');
        if (empty($ids)) {
            $this->error('请选择要操作的数据');
        }

        $map['id'] = array('in', $ids);
        switch ($status) {
            case 'delete' :
                $categoryM = D('Admin/Category');
                $con['cid'] = array('in', $ids);
                $count = D('Article')->where($con)->count();
                if ($count == 0) {
                    $result = $categoryM->where($map)->delete();
                    if ($result) {
                        $this->success('删除分类成功');
                    }
                } else {
                    $this->error('请先删除或移动该分类下文档');
                }
                break;
            default :
                parent::setStatus($model);
                break;
        }
    }
}